﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2017._02._07.DBConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            MsSqlConnection ms = new MsSqlConnection("1", new TimeSpan(100));
            ms.OpenConnection();
            ms.CloseConnection();

            MySqlConnection my = new MySqlConnection("2", new TimeSpan(100));
            my.OpenConnection();
            my.CloseConnection();

            Console.WriteLine();
            DatabaseCommand dc = new DatabaseCommand(ms, "1");
            dc.Execute();
            Console.WriteLine();
            
            DatabaseCommand dbcd = new DatabaseCommand(my, "2");
            dbcd.Execute();
            Console.ReadKey();
        }
    }

    abstract class DatabaseConnection
    {
        private TimeSpan TimeOut { get; set; }
        private string connectionString;

        public string ConnectionString
        { get { return connectionString; }
          private set
            {
                if (String.IsNullOrWhiteSpace(value))
                    throw new ArgumentOutOfRangeException();
                connectionString = value;
            }
        }

        public DatabaseConnection(string connectionString, TimeSpan timeout)
        {
            ConnectionString = connectionString;
            TimeOut = timeout;
        }

        public DatabaseConnection()
        {
            Console.WriteLine("Введите строку подключения: ");
            ConnectionString = Console.ReadLine();
            
        }

        public abstract void OpenConnection();
        public abstract void CloseConnection();
    }

    class MsSqlConnection: DatabaseConnection
    {
        public MsSqlConnection(string connectionString, TimeSpan timeout)
            : base (connectionString,timeout)
        {

        }

        public override void OpenConnection()
        {
            Console.WriteLine("Вы подключились к базе данных MsSql");
        }

        public override void CloseConnection()
        {
            Console.WriteLine("Вы отключились от базы данных MsSql");
        }
    }

    class MySqlConnection: DatabaseConnection
    {
        public MySqlConnection(string connectionString, TimeSpan timeout)
            : base (connectionString,timeout)
        {

        }

        public override void OpenConnection()
        {
            Console.WriteLine("Вы подключились к базе данных MySql");
        }

        public override void CloseConnection()
        {
            Console.WriteLine("Вы отключились от базы данных MySql");
        }
    }

    class DatabaseCommand
    {
        private DatabaseConnection dbc;
        private string sqlString;

        public string SqlString
        { get { return sqlString; }
          private set
            {
                if (String.IsNullOrWhiteSpace(value))
                    throw new ArgumentOutOfRangeException();
                sqlString = value;
            }
        }

        public DatabaseCommand(DatabaseConnection db, string sql)
        {
            if (db == null)
                throw new ArgumentOutOfRangeException();
            if (String.IsNullOrWhiteSpace(sql))
                throw new ArgumentOutOfRangeException();
            dbc = db;
            sqlString = sql;
        }
        

        public void Execute()
        {
            dbc.OpenConnection();
            
            Console.WriteLine(sqlString);
            dbc.CloseConnection();
        }
    }

}
